package com.atguigu.gmall.serviceutil.service.impl;

import com.atguigu.gmall.serviceutil.config.AppMinioProperties;
import com.atguigu.gmall.serviceutil.service.OssService;
import io.minio.MinioClient;
import io.minio.PutObjectOptions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.UUID;

/**
 * @author A
 * @version 1.0
 * @description: TODO
 * @date 2025/8/27 23:48
 */
@Service
public class OssServiceImpl implements OssService {
    @Autowired
    private MinioClient minioClient;
    @Autowired
    private AppMinioProperties appMinioProperties;
    @Override
    public String fileUpload(MultipartFile file) throws Exception {
        //获取文件名
        String fileName = UUID.randomUUID().toString().replaceAll("-", "")+"_"+file.getOriginalFilename();
        //上传文件到minio
        //partSize是分块上传的大小，可以适当调整 -1表示不分块上传
        PutObjectOptions putObjectOptions = new PutObjectOptions(file.getSize(), -1);
        putObjectOptions.setContentType(file.getContentType());
        minioClient.putObject(appMinioProperties.getBucketName(), fileName, file.getInputStream(),putObjectOptions);
        //返回文件路径
        return appMinioProperties.getEndpoint()+"/"+appMinioProperties.getBucketName()+"/"+fileName;
    }
}
